package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmDeviceOnline;
import com.kaili.basic.pojo.entity.MdmDeviceOnlineExport;
import com.kaili.basic.pojo.vo.MdmDeviceOnlineDetailVo;
import com.kaili.basic.pojo.vo.MdmDeviceOnlineVo;
import com.kaili.basic.service.MdmDeviceOnlineService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Description:
 * @Author: shenliqiang
 * @CreateDate: 2019/7/24 11:25
 * @Version: 1.0
 */

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "设备在线管理", description = "设备在线管理相关API")
public class MdmDeviceOnlineController {
    @Autowired
    MdmDeviceOnlineService mdmDeviceOnlineService;

    /**
     * @desc    新增在线设备--finish
     * @author   shenliqiang
     * @param
     * @return
     * @date     2019/7/24 14:19
     */
    @PostMapping(value = "/deviceonline/addDevice")
    @ApiOperation(value = "新增在线设备信息",notes = "新增在线设备",httpMethod = "POST")
    @OperateLogAspect(functionName = FunctionName.deviceOnline,operateType = OperateType.ADD,key = "facilityId")
    public RestStatus addOnlineDevice(@RequestBody MdmDeviceOnlineAddDto mdmDeviceOnlineAddDto){
        return  mdmDeviceOnlineService.addMdmDeviceOnline(mdmDeviceOnlineAddDto);
    }

    /**
     * @desc    更新设备在线信息--finish
     * @author   shenliqiang
     * @param
     * @return
     * @date     2019/7/24 14:22
     */
    @PutMapping(value = "/deviceonline/updateDevice")
    @ApiOperation(value = "修改在线设备",notes = "修改在线设备，仅传入需要修改的参数即可--设备接口")
    @OperateLogAspect(functionName = FunctionName.deviceOnline,operateType = OperateType.UPDATE,key = "facilityId")
    public RestStatus updateOnlineDevice(@RequestBody MdmDeviceOnlineUpdateDto mdmDeviceOnlineUpdateDto){
        return  mdmDeviceOnlineService.updateMdmDeviceOnline(mdmDeviceOnlineUpdateDto);
    }

    /**
     * @desc 删除在线设备--finished
     * @author   shenliqiang
     * @param
     * @return
     * @date     2019/7/24 14:23
     */
    @DeleteMapping("/deviceonline/{deviceIds}/deleteDevices")
    @ApiOperation(value = "删除在线设备", notes = "删除在线设备")
    @OperateLogAspect(functionName = FunctionName.deviceOnline,operateType = OperateType.DELETE,key = "facilityId")
    public RestStatus deleteOnlineDevices(@PathVariable Long[] deviceIds) {
        return  mdmDeviceOnlineService.deleteMdmDeviceOnlines(deviceIds);
    }

    /**
    * @Desc:  批量查询基本信息--finished
    * @author: shenliqiang
    * @Time: 2019/7/25 13:40
    * @param mdmDeviceOnlineQueryDto
    * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmDeviceOnlineVo>
    */

    @GetMapping(value = "deviceonline/queryDevices")
    @ApiOperation(value = "条件查询在线设备信息",notes = "条件查询在线设备信息，列表界面")
    public Page<MdmDeviceOnlineVo> queryonlineDevice(@ModelAttribute MdmDeviceOnlineQueryDto mdmDeviceOnlineQueryDto) {
        Page<MdmDeviceOnlineVo> page = mdmDeviceOnlineService.queryMdmDeviceOnline(new Page<>(mdmDeviceOnlineQueryDto.getPage(),mdmDeviceOnlineQueryDto.getPageSize()),mdmDeviceOnlineQueryDto);
        return page;
    }

    /**
    * @Desc:  查询在线设备的详细信息--finished
    * @author: shenliqiang
    * @Time: 2019/7/25 11:25
    * @param deviceOnlineId
    * @return com.kaili.basic.pojo.vo.MdmDeviceOnlineDetailVo
    */

    @GetMapping(value = "/deviceonline/{deviceOnlineId}/queryDevices")
    @ApiOperation(value = "根据id查询设备的所有在线信息",notes = "根据id查询设备的所有在线信息，获取设备详情")
    @ResponseBody
    public MdmDeviceOnlineDetailVo queryOnlineDeviceDetail(@PathVariable Long deviceOnlineId) {
        return mdmDeviceOnlineService.queryOnlineDeviceDetail(deviceOnlineId);
    }

    /**
     * @desc 导出设备--finished
     * @author   shenliqiang
     * @param
     * @return
     * @date     2019/7/24 14:23
     */
    @GetMapping("/deviceonline/export")
    @ApiOperation(value = "导出在线设备信息")
    public void export(HttpServletResponse response, @ModelAttribute MdmDeviceOnlineQueryDto mdmDeviceOnlineQueryDto) throws RestException {
        List<MdmDeviceOnlineExport> list = mdmDeviceOnlineService.queryMdmDeviceOnlineExport(mdmDeviceOnlineQueryDto);
        ExcelUtil.exportExcel(list,"在线设备信息","sheet1", MdmDeviceOnlineExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}